package main

import (
	"context"
	"gitee.com/qzcsu/go-web-study/utils/jsonutil"
	"log"
	"time"

	"gitee.com/qzcsu/go-web-study/grpc/proto/pandago"

	"google.golang.org/grpc"
)

const (
	address = "localhost:8997"
)

func main() {
	conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	c := pandago.NewPandaGoServiceClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	req := getReq()
	resp, err := c.SayHello(ctx, req)
	if err != nil {
		log.Fatalf("could not SayHello: %v", err)
	}
	log.Printf("HelloReply: %+v", resp)
}

func getReq() *pandago.HelloRequest {
	req := &pandago.HelloRequest{
		Name:       "小明",
		Age:        20,
		LikeSeason: pandago.Season_Summer,
		IsStudent:  true,
		Whisper:    []byte("i love you !!!"),
	}
	hobby := []*pandago.Hobby{
		{
			HobbyID:   10,
			HobbyName: "football",
		}, {
			HobbyID:   13,
			HobbyName: "eat",
		},
	}
	families := map[string]*pandago.HelloRequest_Family{
		"father": {
			Name:     "小明爸",
			NickName: "dad",
			Age:      60,
		},
		"mother": {
			Name:     "小明妈",
			NickName: "mom",
			Age:      55,
		},
	}
	req.Hobby = hobby
	req.Families = families
	log.Printf("json req:%v\n", jsonutil.StructToJSON(req))
	return req
}
